package com.jic.market.impl.xoffer;

import com.jic.common.base.vo.RestResult;
import com.jic.market.common.base.MarketBaseService;
import com.jic.market.entity.*;
import com.jic.market.enums.PromotionStatusEnum;
import com.jic.market.enums.PromotionTypeEnum;
import com.jic.market.mapper.*;
import com.jic.market.request.manager.presell.PromotionPresellCopyRequest;
import com.jic.market.request.manager.xoffer.PromotionXofferCopyRequest;
import com.jic.market.service.PromotionTemplateService;
import com.jic.market.service.xoffer.PromotionXofferService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PromotionXofferServiceImpl extends MarketBaseService implements PromotionXofferService {
    @Autowired
    private PromotionTemplateService promotionTemplateService;
    @Resource
    private PromotionEntityMapper promotionEntityMapper;
    @Resource
    private PromotionPresellRuleMapper promotionPresellRuleMapper;
    @Resource
    private PromotionMerchantEntityMapper promotionMerchantEntityMapper;
    @Resource
    private PromotionPresellProductMapper promotionPresellProductMapper;
    @Resource
    PromotionRuleEntityMapper promotionRuleEntityMapper;
    @Autowired
    private PromotionGiftItemEntityMapper promotionGiftItemEntityMapper;

    @Override
    public RestResult<Long> copyXofferPromotion(PromotionXofferCopyRequest request) {
        return executeQuery(() -> {
            // 复制配置信息，返回新的活动id
            PromotionTemplateService.CopyResult copyResult = copyConfig(request, PromotionStatusEnum.DRAFT, "-复制");
            if (null != copyResult) {
                return Long.valueOf(copyResult.getPromotionId());
            }
            return null;
        });
    }


    /**
     * 复制方法
     *

     * @param status         复制需要存储的状态
     * @param extenPromName  对应的活动扩展名
     * @return
     */
    private PromotionTemplateService.CopyResult copyConfig(PromotionXofferCopyRequest request, PromotionStatusEnum status, String extenPromName) {
        // 复制主表
        Long oldPromotionId =request.getPromotionId();
        PromotionEntity promotionEntityQ = new PromotionEntity();
        promotionEntityQ.setId(oldPromotionId);
        PromotionEntity promotionEntity = promotionEntityMapper.query(promotionEntityQ);
        if (null == promotionEntity) {
            return null;
        }
        promotionEntity.setId(null);
        promotionEntity.setStatus(status.getCode());
        if(PromotionStatusEnum.TEMPLATE.equals(status)){
            promotionEntity.setPromName(extenPromName);
        }else {
            promotionEntity.setPromName(promotionEntity.getPromName() + extenPromName);
        }
        promotionEntity.setCreateTime(new Date());
        promotionEntity.setUpdateTime(new Date());
        int flag = promotionEntityMapper.insert(promotionEntity);
        if (0 == flag) {
            return null;
        }
        //结果组装
        PromotionTemplateService.CopyResult copyResult = new PromotionTemplateService.CopyResult();
        copyResult.setPromotionId(promotionEntity.getId()+"");
        copyResult.setPromType(PromotionTypeEnum.getByCode(promotionEntity.getPromType()));
        copyResult.setTemplateName(promotionEntity.getPromName());

        // 复制规则配置表
        PromotionRuleEntity ruleEntity = new PromotionRuleEntity();
        ruleEntity.setPromotionId(oldPromotionId);
        ruleEntity.setDeleteFlag(0);
        List<PromotionRuleEntity> ruleEntityList = promotionRuleEntityMapper.findAll(ruleEntity);
        if(CollectionUtils.isNotEmpty(ruleEntityList)) {
            ruleEntityList.forEach(x -> {
                x.setId(null);
                x.setCreateTime(null);
                x.setUpdateTime(null);
                x.setPromotionId(promotionEntity.getId());
                promotionRuleEntityMapper.insert(x);
            });
        }
        // 复制商家店铺表
        PromotionMerchantEntity merchantEntityQ = new PromotionMerchantEntity();
        merchantEntityQ.setPromotionId(oldPromotionId);
        List<PromotionMerchantEntity> promotionMerchantEntityList = promotionMerchantEntityMapper.findAll(merchantEntityQ);
        if (null != promotionMerchantEntityList && !promotionMerchantEntityList.isEmpty()) {
            for (PromotionMerchantEntity merchantEntity : promotionMerchantEntityList) {
                merchantEntity.setId(null);
                merchantEntity.setPromotionId(promotionEntity.getId());
                merchantEntity.setCreateTime(new Date());
                merchantEntity.setUpdateTime(new Date());
                promotionMerchantEntityMapper.insert(merchantEntity);
            }
        }

        // 复制商品表
        PromotionPresellProduct presellProductQ = new PromotionPresellProduct();
        presellProductQ.setPromotionId(oldPromotionId);
        List<PromotionPresellProduct> presellProductList = promotionPresellProductMapper.query(presellProductQ);
        if (null != presellProductList && !presellProductList.isEmpty()) {
            for (PromotionPresellProduct presellProduct : presellProductList) {
                presellProduct.setId(null);
                presellProduct.setPromotionId(promotionEntity.getId());
                presellProduct.setCreateTime(new Date());
                presellProduct.setUpdateTime(new Date());
                promotionPresellProductMapper.insert(presellProduct);
            }
        }

        //复制promotion_gift_item表
        PromotionGiftItemEntity promotionGiftItemEntity = new PromotionGiftItemEntity();
        promotionGiftItemEntity.setPromotionId(oldPromotionId);
        promotionGiftItemEntity.setDeleteFlag(0);
        List<PromotionGiftItemEntity> giftItemEntities = promotionGiftItemEntityMapper.findAll(promotionGiftItemEntity);
        if (CollectionUtils.isNotEmpty(giftItemEntities)) {
            giftItemEntities.forEach(gi -> {

            });
        }
        return copyResult;
    }

    //保存模板
    @Transactional(rollbackFor = Exception.class)
    @Override
    public RestResult<Long> saveTemplate(PromotionXofferCopyRequest request) {
        return executeQuery(() -> {
            Long templateId = promotionTemplateService.saveTemplate(() -> {

                // 复制配置信息
                PromotionTemplateService.CopyResult copyResult = copyConfig(request, PromotionStatusEnum.TEMPLATE, request.getTemplateName());
                copyResult.setAdminOperate(request.getAdminOperate());
                return copyResult;
            });
            return templateId;
        });
    }

}
